﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class public_html_NewSiteTest_Releaseliste : System.Web.UI.Page
{
    public string SpilID = "";
    public string Format = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        DateFromMySql NewDate = new DateFromMySql();
        ReviewerMySql SQL = new ReviewerMySql();
        string SpilID = Request.QueryString["id"];
        int Count = 0;
        int i = 0;

        imgBarometer.ImageUrl = "http://www.reviewer.dk/spilBarometer.aspx?id=" + SpilID;

        Reviewer.Games.Games.GameCount(int.Parse(SpilID));

        OdbcDataReader MainReader = SQL.ExecuteReader("SELECT V2_Spil.*, V2_GloFormat.Navn AS FormatNavn, V2_GloGenres.Navn AS GenreNavn FROM V2_Spil JOIN V2_GloFormat ON V2_GloFormat.id = V2_Spil.Format JOIN V2_GloGenres ON V2_GloGenres.id = V2_Spil.Genre WHERE V2_Spil.id = " + SpilID);
        MainReader.Read();

        OdbcDataReader TopReader = SQL.ExecuteReader("SELECT * FROM V2_SpilTopBilled WHERE SpilID = '" + MainReader["SpilID"].ToString() + "'");
        if (TopReader.Read())
        {
            DivTopBilled.Attributes.Add("style", "background-image:url(/SpilTop/" + MainReader["SpilID"].ToString() + ".jpg); width:600px; height:150px; overflow:hidden;");
            lblOverskrift.Visible = false;
            if (TopReader["TextInLogo"].ToString() == "False")
            {
                lblImg.Text = "&nbsp;" + MainReader["Titel"].ToString();
                if (TopReader["TextInLogo"].ToString() == "#FFFFFF")
                {
                    lblImg.ForeColor = System.Drawing.Color.White;
                }
                if (TopReader["TextInLogo"].ToString() == "#000000")
                {
                    lblImg.ForeColor = System.Drawing.Color.White;
                }
            }
        }
        else
        {
            DivTopBilled.Visible = false;
        }

        lblOverskrift.Text = MainReader["Titel"].ToString() + " (" + MainReader["FormatNavn"].ToString() + ")";

        lblGenre.Text = MainReader["GenreNavn"].ToString();

        if (MainReader["Multi"].ToString() == String.Empty)
        {
            lblMulti.Text = "<i>Oplysning mangler...</i>";
        }
        else
        {
            lblMulti.Text = MainReader["Multi"].ToString();
        }
        
        if (MainReader["Udgiver"].ToString() == String.Empty)
        {
            lblUdgiver.Text = "<i>Oplysning mangler...</i>";
        }
        else
        {
            lblUdgiver.Text = MainReader["Udgiver"].ToString();
        }

        if (MainReader["Udvikler"].ToString() == String.Empty)
        {
            lblUdvikler.Text = "<i>Oplysning mangler...</i>";
        }
        else
        {
            lblUdvikler.Text = MainReader["Udvikler"].ToString();
        }

        if (NewDate.GetDate(MainReader["ReleaseDato"].ToString()) == String.Empty)
        {
            lblDato.Text = "<i>TBA</i>";
        }
        else
        {
            lblDato.Text = NewDate.GetDate(MainReader["ReleaseDato"].ToString());
        }

        string coverpath = Server.MapPath(@"covers/");
        if (File.Exists(coverpath + MainReader["SpilID"].ToString() + "_" + MainReader["Format"].ToString() + ".jpg"))
        {
            imgCover.ImageUrl = "covers/" + MainReader["SpilID"].ToString() + "_" + MainReader["Format"].ToString() + ".jpg";
        }
        else
        {
            imgCover.ImageUrl = "App_Themes/RV2/Images/cover" + MainReader["Format"].ToString() + ".jpg";
        }

        DivOther.InnerHtml += "&nbsp;&nbsp;";

        OdbcDataReader FormatReader = SQL.ExecuteReader("SELECT V2_Spil.id, V2_Spil.Titel as Spilnavn, V2_GloFormat.Navn FROM V2_Spil JOIN V2_GloFormat ON V2_GloFormat.id = V2_Spil.Format WHERE SpilID = '" + MainReader["SpilID"].ToString() + "' AND V2_Spil.Format <> " + MainReader["Format"].ToString());
        while (FormatReader.Read())
        {
            if (Count != 0)
            {
                DivOther.InnerHtml += ", ";
            }
            DivOther.InnerHtml += String.Format(
                "<a href=\"{0}\">{1}</a>"
                , UI.LinkMake.LinkSpil(FormatReader["id"].ToString(), FormatReader["Spilnavn"].ToString())
                , FormatReader["Navn"].ToString()
                );

            //"<a href=\"Spil.aspx?id=" + FormatReader["id"].ToString() + "\">" + FormatReader["Navn"].ToString() + "</a>";

            Count++;
        }

        if (Count != 0)
        {
            DivOther.InnerHtml += ".";
        }

        OdbcDataReader ScreensReader = SQL.ExecuteReader("SELECT * FROM V2_Screenshots WHERE SpilID = '" + MainReader["SpilID"].ToString() + "' AND Format = '" + MainReader["Format"].ToString() + "'");
        string ScreensID = "";
        string ScreensFo = "";
        while (ScreensReader.Read())
        {
            DivContentScreens.InnerHtml += "<a href=\"Screenshot.aspx?PicID=" + ScreensReader["ID"].ToString() + "&SpilID=" + ScreensReader["SpilID"].ToString() + "&Format=" + MainReader["Format"].ToString() + "\"><img border=\"0\" style=\"padding:2px;\" width=\"140px\" src=\"screenshots/" + ScreensReader["Filnavn"].ToString() + "\"></a>";
            ScreensFo = ScreensReader["Format"].ToString();
            ScreensID = ScreensReader["SpilID"].ToString();
            i++;
        }
        if (i == 0)
        {
            DivContentScreens.InnerHtml = "<center><i>p.t. er der desværre ingen Screenshots</i></center>";
        }
        
        i = 0;

        OdbcDataReader RelationReaderReviewes = SQL.ExecuteReader("SELECT * FROM V2_Anmeldelse WHERE SpilID = '" + MainReader["ID"].ToString() + "' ORDER BY id DESC");
        while (RelationReaderReviewes.Read())
        {
            DivContentRelater.InnerHtml += String.Format("<i>{0}</i><br /><a href=\"{1}\"><b>{2} Anmeldelse</b></a><br /><br />"
                , NewDate.GetDate(RelationReaderReviewes["Dato"].ToString())
                , UI.LinkMake.LinkReview(RelationReaderReviewes["ID"].ToString(), MainReader["Titel"].ToString())
                , MainReader["Titel"].ToString());
                
                
                //"<i>" + NewDate.GetDate(RelationReaderReviewes["Dato"].ToString()) + "</i><br /><a href=\"Anmeldelse.aspx?id=" + RelationReaderReviewes["ID"].ToString() + "\"><b>" + MainReader["Titel"].ToString() + " Anmeldelse</b></a><br /><br />";
            i++;
        }
    
        OdbcDataReader RelationReader = SQL.ExecuteReader("SELECT V2_NewsRelations.*, V2_Nyhed.Titel AS NyhedTitel, V2_Nyhed.Dato AS NyhedDato, V2_Nyhed.id AS NyhedID FROM V2_NewsRelations JOIN V2_Nyhed ON V2_NewsRelations.NewsID = V2_Nyhed.NewsID WHERE V2_NewsRelations.SpilID = '" + MainReader["SpilID"].ToString() + "' ORDER BY Dato DESC");
        while(RelationReader.Read()) 
        {
            DivContentRelater.InnerHtml += String.Format("<i>{0}</i><br /><a href=\"{1}\">{2}</a><br /><br />"
                , NewDate.GetDate(RelationReader["NyhedDato"].ToString())
                , UI.LinkMake.LinkNews(RelationReader["NyhedID"].ToString(), RelationReader["NyhedTitel"].ToString())
                , RelationReader["NyhedTitel"].ToString());

                //"<i>" + NewDate.GetDate(RelationReader["NyhedDato"].ToString()) + "</i><br /><a href=\"Nyhed.aspx?id=" + RelationReader["NyhedID"].ToString() + "\">" + RelationReader["NyhedTitel"].ToString() + "</a><br /><br />";
            i++;
        }      

        if(i == 0) 
        {
            DivContentRelater.InnerHtml = "<center><i>p.t. er der desværre intet relateret indhold</i></center>";
        }
        string Vidlink = "";
        int Vidcounter = 0;
        OdbcDataReader VideoReader = SQL.ExecuteReader("SELECT Titel,ID FROM V2_SpilVideo WHERE SpilID = '" + MainReader["SpilID"].ToString() + "' ORDER BY ID DESC ");
        while (VideoReader.Read())
        {
            Vidlink = UI.LinkMake.LinkVideo(VideoReader[1].ToString(), MainReader["Titel"].ToString() + " " + VideoReader[0].ToString());
            Vidcounter++;
            DivTrailersContent.InnerHtml += "<a href=\"" + Vidlink + "\">" + VideoReader[0].ToString() + "</a>";
        }

        if (Vidcounter == 0)
        {
            DivTrailers.Visible = false;
        }
        //HtmlMeta HeaderDescription = new HtmlMeta();
        //HeaderDescription.Name = "description";
        //HeaderDescription.Content = "Spilinformation - " + MainReader["Titel"].ToString() + " til " + MainReader["FormatNavn"].ToString();
        //this.Header.Controls.Add(HeaderDescription);
        this.Title = "Spil Information: " + MainReader["Titel"].ToString() + " til " + MainReader["FormatNavn"].ToString();
    }
}
